package com.sand.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sand.Exception.SandException;
import com.sand.dto.Select.UserSelect;
import com.sand.dto.merchant.AddMerchant;
import com.sand.dto.merchant.merchantDto;

import com.sand.entity.user.Merchant;
import com.sand.entity.user.User;
import com.sand.feign.UserFeign;
import com.sand.mapper.MerchantMapper;
import com.sand.merchant.Permission;
import com.sand.result.AppHttpCodeEnum;
import com.sand.result.ResponseResult;
import com.sand.service.MerchantService;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Slf4j
@Service
public class MerchantServiceImpl extends ServiceImpl<MerchantMapper, Merchant> implements MerchantService {

    @Autowired
    private UserFeign userFeign;


    /**
     * 新增商家
     *
     */
    @Override
    public String  add(AddMerchant addMerchant) {

        //获取id
        Integer userid = addMerchant.getUserid();

        //获取权限
        Integer permission = addMerchant.getPermission();

        if (userid == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"没有传入用户id");
        }

        //根据id查询用户
        User user = userFeign.getById(userid).getData();


        if (permission == null){
            //没有传入权限默认无权限
            addMerchant.setPermission(Permission.NO);
        }


        if (user == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"不存在该用户");
        }

        //保存
        Merchant merchant = new Merchant();

        merchant.setUserid(userid);
        merchant.setPermission(permission);

        save(merchant);

        return "添加商家成功";
    }


    /**
     * 查看商家
     *      1.传入的参数:
     *           id : 商家表id
     *
     *           phone : 商家电话
     *
     */
    public merchantDto select(UserSelect userSelect) {

        Integer id = userSelect.getId();

        String phone = userSelect.getPhone();

        //两个都没传入
        if (id == null && phone.isEmpty()) {
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID, "未传入要查寻的用户id或号码");
        }

        if (id != null && (phone == null || phone.isEmpty())) {
            //根据商家表的userid查询
            Merchant merchant = getOne(new LambdaQueryWrapper<Merchant>().eq(Merchant::getUserid,id));
            if (merchant == null) {
                throw new SandException(AppHttpCodeEnum.PARAM_INVALID, "该商家不存在");
            }

            //获得商家的userid
            Integer userid = merchant.getUserid();

            // 根据userid查询商家基本信息
            ResponseResult<User> inquire = userFeign.inquire(new UserSelect(userid, null));

            //拼接
            return getMerchantDto(inquire, merchant);
        }


        if (id == null) {
            //只传了电话号码
            ResponseResult<User> inquire = userFeign.inquire(new UserSelect(null, phone));

            //根据电话号码获取人员信息
            if (inquire.getData() == null) {
                throw new SandException(AppHttpCodeEnum.PARAM_INVALID, "该电话号码不存在");
            }

            //获取人员信息中的id
            Integer Uid = inquire.getData().getId();

            //根据id找到商家
            Merchant merchant = getOne(new LambdaQueryWrapper<Merchant>().eq(Merchant::getUserid, Uid));

            //拼接
            return getMerchantDto(inquire, merchant);
        }

        //两个都传入
        //根据商家表id查询
        Merchant merchant = getById(id);
        if (merchant == null) {
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID, "该商家不存在");
        }

        //根据电话号码获取人员信息
        ResponseResult<User> inquire = userFeign.inquire(new UserSelect(null, phone));
        if (inquire.getData() == null) {
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID, "该电话号码不存在");
        }

        return getMerchantDto(inquire, merchant);


    }

    /**
     * 根据商家id查询商家的权限
     */
    @Override
    public Integer permission(Integer id) {

        //获取商家
        Merchant merchant = getMerchant(id);

        //返回id权限
        return merchant.getPermission();
    }

    //修改商家权限
    @Override
    public String UpdatePermission(Integer id) {

        //获取商家
        Merchant merchant = getMerchant(id);

        if (merchant.getPermission() == 0){
            merchant.setPermission(1);
        }else {
            merchant.setPermission(0);
        }

        //修改权限
        updateById(merchant);

        return "修改商家权限成功";
    }

    /**
     * 用于拼接商家和人员信息
     * @param inquire
     * @param merchant
     * @return
     */
    private static merchantDto getMerchantDto(ResponseResult<User> inquire, Merchant merchant) {
        merchantDto merchantDto = new merchantDto();

        BeanUtils.copyProperties(inquire.getData(), merchantDto);
        BeanUtils.copyProperties(merchant, merchantDto);

        return merchantDto;
    }


    //根据id获取商家
    @NotNull
    private Merchant getMerchant(Integer id) {
        if (id == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"没有传入商家id");
        }

        //根据id获取商家
        Merchant merchant = getOne(new LambdaQueryWrapper<Merchant>().eq(Merchant::getUserid,id));

        if (merchant == null) {
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"找不到商家");
        }
        return merchant;
    }


}







